﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skynet.BusinessLogic.DataAccess;
using Microsoft.Practices.Unity;

using Skynet.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;

namespace Skynet.BusinessLogic.DataAccess
{
    public class ExamCategoryRepository : MasterRepository<ExamCategory>, IExamCategoryRepository
    {
        public ExamCategoryRepository(IUnityContainer container): base(container)
        {

        }
        public override  void Add(ExamCategory item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            ctx.ExamCategories.ApplyChanges(item);
            ctx.SaveChanges();
        }
        public void Maintenance(ExamCategory item) {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            ctx.ExamCategories.ApplyChanges(item);
            ctx.SaveChanges();
            item.AcceptChanges();
        }
        public override IEnumerable<ExamCategory> GetAll()
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();            
            var query = (from c in ctx.ExamCategories select c);            
            return query.ToList();
        }
        public override ExamCategory GetOne(object key)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query =  (from c in ctx.ExamCategories where c.Id == (Guid)key select c).SingleOrDefault();
            
            return query;
        }
        public override void Add(IEnumerable<ExamCategory> items)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            foreach (var item in items)
            {
                ctx.ExamCategories.AddObject(item);
                
            }            
            ctx.SaveChanges();
            foreach (var item in items)
            {
                item.AcceptChanges();
            }            
        }

        public override void Update(ExamCategory item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.ExamCategories where c.Id == item.Id select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");
            ctx.ExamCategories.ApplyCurrentValues(item);            
            ctx.SaveChanges();
            item.AcceptChanges();
        }
  
        public override void Delete(ExamCategory item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.ExamCategories where c.Id == item.Id  select c).SingleOrDefault();
            if (query != null)
            {
                ctx.ExamCategories.DeleteObject(query);
                ctx.SaveChanges();
                item.AcceptChanges();
            }
        }
        
        public System.Data.IDataReader GetAllStream()
        {
             var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();            
            System.Data.IDataReader reader   = null;
            System.Data.Common.DbConnection con = null;
            System.Data.Common.DbProviderFactory factory = null;  
            try
            {

                if (((EntityConnection)ctx.Connection).StoreConnection is System.Data.SqlClient.SqlConnection)
                {
                    factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
                    con = factory.CreateConnection();
                    con.ConnectionString = ((EntityConnection)ctx.Connection).StoreConnection.ConnectionString;  
                }              
                var cmd = con.CreateCommand();
                cmd.CommandText = "usp_ExamCategories_GetAll";
                cmd.CommandType = CommandType.StoredProcedure;  
                con.Open();
                reader = cmd.ExecuteReader( CommandBehavior.CloseConnection);  
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.Aspect.ExceptionHandling.ExceptionPolicyNames.DataAccessPolicy);
                if (rethrow)
                    throw;                
            }
            return reader;  
        }


        public System.Data.DataSet GetTable()
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();

            System.Data.Common.DbProviderFactory factory = null;  
            DataSet result = new DataSet();  
            System.Data.Common.DbConnection con = null;
            try
            {
                if (((EntityConnection)ctx.Connection).StoreConnection is System.Data.SqlClient.SqlConnection)
                {
                    factory = DbProviderFactories.GetFactory("System.Data.SqlClient");                    
                    con = factory.CreateConnection();
                    con.ConnectionString = ((EntityConnection)ctx.Connection).StoreConnection.ConnectionString;
                }                
                var cmd = con.CreateCommand();
                cmd.CommandText = "usp_ExamCategories_GetAll";
                cmd.CommandType = CommandType.StoredProcedure;
                var adp = factory.CreateDataAdapter();                
                adp.SelectCommand = cmd;
                adp.Fill(result, "ExamCategories");                
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.Aspect.ExceptionHandling.ExceptionPolicyNames.DataAccessPolicy);
                if (rethrow)
                    throw;
            }
            return result;  
        }


        DataTable IExamCategoryRepository.GetTable()
        {
            throw new NotImplementedException();
        }

        public void UpdateTable(DataTable ds)
        {
            throw new NotImplementedException();
        }
    }
}
